﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Data.Lexing
{
    public class Parser
    {
        public Parser(string sou) : this(new Lexer(sou))
        {

        }
        Lexer lexer;
        public Parser(Lexer lexer)
        {
            this.lexer = lexer;
            tokens = lexer.Reslove();
        }

        protected Token[] tokens;

        protected uint cur = 0;

        public bool EOF()
        {
            if (tokens.Length == 0)
            {
                return true;
            }
            if (cur >= tokens.Length)
            {
                return true;
            }
            return false;
        }
        protected void err(string msg)
        {
            throw new Exception(msg);
        }
        public Token Next()
        {
            cur++;
            if (EOF())
            {
                err("EOF!");
            }
            return tokens[cur];
        }
        public Token Previous()
        {
            cur--;
            if (EOF())
            {
                err("EOF!");
            }
            return tokens[cur];

        }

        public Token Current
        {
            get
            {
                if (EOF())
                {
                    err("EOF!");
                }
                return tokens[cur];
            }
        }

        public Token Preview(int offset=1)
        {
            var ni = cur + offset;
            if (ni<0 || ni>=tokens.Length)
            {
                err("EOF!");
            }
            return tokens[ni];
        }

    }
}
